﻿<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta name="renderer" content="webkit">
    <meta http-equiv="CurrData.Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <title>Index </title>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />

    <link href="/js/kd/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="/js/kd/styles/kendo.blueopal.min.css" rel="stylesheet" />
    <link href="/js/fa/css/font-awesome.min.css" rel="stylesheet" />
    <link href="/js/site.css?t=20221031" rel="stylesheet" />

    <script src="/js/kd/jquery.min.js"></script>
    <script src="/js/kd/kendo.web.min.js"></script>
    
    <script src="/js/kd/cultures/kendo.culture.zh-CN.min.js"></script>
    <script src="/js/kd/messages/kendo.messages.zh-CN.min.js"></script>
    <script src="/js/kd/jszip.min.js"></script>

    
    <script src="/js/kendo_stdefine.js"></script>
    <script src="/js/site50.js"></script>

    <script src="/js/xlsx.full.min.js?t=1"></script>


</head>
<body>

    <div id="divSplitter" data-role="splitter"
         data-panes="[
                { collapsible: false, resizable: false },
                { collapsible: false, resizable: false,  size: '50px' }
             ]"
         data-orientation="vertical"
         >

        <div id="center" style="overflow:hidden;padding-top:0px;overflow-y: auto;">
            <div style="height:10px;"></div>
            <form id="form1"  >
                <div class="flexrow">
                    <div class="tdLabel75 Required">名称</div>
                    <div class="tdValue">

                        <input id="State"
                               data-role="combobox"
                               data-value-primitive="true"
                               data-text-field="Name"
                               data-value-field="Name"
                               data-auto-bind="true"
                               data-bind="source: NameSource,value:m.Name" />

                    </div>
                    <div style="width:20px;"></div>
                </div>
            </form>
            <div style="height:10px;"></div>
            <div id="grid" style="height: calc(100% - 60px)"></div>

        </div>

        <div id="bottom" class="flexrow" style="  ">

            <div style="flex:1;">


            </div>
            <div style="width:100px;">
                <input type="file" id="fileInputSJZD" style="display:none;" accept=".xlsx" />
                <a id="btnImport" class="k-button"
                   style="cursor:pointer;margin-left:10px;" title="导入"
                   onclick="document.getElementById('fileInputSJZD').click()"><i class="fa   fa-upload"></i>导入</a>

            </div>

            <div style="width:100px;">
                <a id="btnExport" class="k-button"
                   style="cursor:pointer;margin-left:10px;" title="导出"
                   onclick="exportToExcelSJZD()"><i class="fa  fa-download"></i>导出</a>
            </div>

            <div style="width:100px;">
                <a href="javascript:btnOKonClick();" class="k-button" style="width:80px; " accesskey="S">

                    <i class="fa fa-save"></i>
                    <span style="text-decoration:underline;margin-left:5px;">S</span>
                    确定
                </a>
            </div>
            <div style="width:80px;">
                <a href="javascript:myWindowClose();" class="k-button" style="width: 80px;  " accesskey="C">
                    <i class="fa fa-close"></i>
                    <span style="text-decoration: underline; margin-left: 5px;">C</span>
                    取消

                </a>
            </div>
            <div style="width:20px;"></div>

        </div>

    </div>



        <script>
            var IsAdd = false;
            var grid;
            var viewModel = new kendo.data.ObservableObject({
                NameSource: [
                    { Name: "监测类型" },
                    { Name: "监测单位" },
                    { Name: "设备状态" },
                    { Name: "开关量状态" },
                ],
                BJStatusSource: [
                    { Name: "正常" },
                    { Name: "故障" },
                    { Name: "一级报警" },
                    { Name: "二级报警" },
                ],
                m: {
                    Name: "",
                    Id:""
                },
            });

            function BJStatusEditor(container, options) {
                $('<input required name="BJStatus">')
                    .appendTo(container)
                    .kendoDropDownList({
                        autoBind: false,
                        dataTextField: "Name",
                        dataValueField: "Name",
                        dataSource: {
                            data: viewModel.BJStatusSource
                        }
                    });
            }
            function IMinEditor(container, options) {
                $('<input   name="IMin">')
                    .appendTo(container)
                    .kendoNumericTextBox({
                        max: 65535,
                        min: 0
                    });
            }
            function IMaxEditor(container, options) {
                $('<input   name="IMax">')
                    .appendTo(container)
                    .kendoNumericTextBox({
                        max: 65535,
                        min: 0
                    });
            }

            function Init() {
                $("#form1").kendoValidator(kendoValidatorOptions)
                kendo.bind($("#divSplitter"), viewModel); 

                var opt = Object.assign({}, gridBaseOption);
                opt.editable = true;
                opt.columns = [
                    { title: '名字', field: "Name", width: 50, sortable: false, filterable: false, },
                    {
                        title: '低值', field: "IMin", width: 50, sortable: false, filterable: false,
                        editor: IMinEditor
                    },
                    {
                        title: '高值', field: "IMax", width: 50, sortable: false, filterable: false,
                        editor: IMaxEditor
                    },
                    {
                        title: '报警状态', field: "BJStatus", width: 50, sortable: false, filterable: false,
                        editor: BJStatusEditor
                    }

                ]
                grid = $("#grid").kendoGrid(opt).data("kendoGrid");
            }
            function btnOKonClick() {

                var v = $("#form1").data("kendoValidator");
                if (v.validate() == false) return;

                var ds = grid.dataSource;
                var data = ds.data();
                var data2 = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].Name) {
                        if (data[i].IMin && data[i].IMax) {
                            if (data[i].IMin > data[i].IMax) {
                                myAlert("数据错误");
                                return;
                            }
                        }
                        data2.push(data[i]);
                    }
                }

                data = data2;
                

                for (var i = 0; i < data.length; i++) {
                    if (data[i].IMin != null && data[i].IMax != null) {
                        if (data[i].IMin > data[i].IMax) {
                            myAlert(`最小值最大值错误 ${data[i].IMin}:${data[i].IMax}`);
                        }
                    }
                }
                // 检查区间是否重复
                for (let i = 0; i < data.length; i++) {
                    for (let j = i + 1; j < data.length; j++) {
                        if (data[i].IMin < data[j].IMax && data[i].IMax > data[j].IMin) {
                            myAlert(`区间重复: [${data[i].IMin}, ${data[i].IMax}] 与 [${data[j].IMin}, ${data[j].IMax}]`);
                            return;
                        }
                    }
                }


                var m = viewModel.m.toJSON();
                m.Data = data ; 
                 
                onOk(m );
            }
            function setData(m) {
                viewModel.set("m", m);

                var dsoptions = {
                    schema: {
                        model: {
                            id: "Id",
                            fields: { Id: { editable: false, nullable: true } }
                        }
                    },
                    data: m.Data,
                };
                var gridDataSource = new kendo.data.DataSource(dsoptions);
                grid.setDataSource(gridDataSource);
                gridDataSource.read();
            }




            // 添加文件读取功能
            document.getElementById('fileInputSJZD').addEventListener('change', function (e) {
                var file = e.target.files[0];
                if (!file) return;

                var reader = new FileReader();
                reader.onload = function (e) {
                    try {
                       
                        var data = new Uint8Array(e.target.result);
                        var workbook = XLSX.read(data, { type: 'array' });
                        var firstSheetName = workbook.SheetNames[0];
                        var worksheet = workbook.Sheets[firstSheetName];
                        var json = XLSX.utils.sheet_to_json(worksheet, {
                            header: 1,  // 使用数组格式
                            defval: "", // 空单元格默认值
                            raw: false, // 处理公式和格式
                            blankrows: false // 包含空行
                        });

                        // 处理读取的数据
                        if (json.length > 1) {
                            // 这里添加处理逻辑，将数据填充到表格中
                            var data = []
                            for (var i = 1; i < json.length; i++) {

                                var m = {
                                    Name: json[i][0],
                                    IMin: null,
                                    IMax: null,
                                    BJStatus: ""
                                };
                                if (json[i][1].toString() != "") {
                                    m.IMin = parseInt(json[i][1]);
                                }
                                if (json[i][2].toString() != "") {
                                    m.IMax = parseInt(json[i][2]);
                                }

                                if (json[i][3].toString() != "") {
                                    var s = json[i][3].toString();
                                    if (viewModel.BJStatusSource.some(item => item.Name === s)) {
                                        m.BJStatus = s;
                                    }
                                }
                                data.push(m);
                                viewModel.m.Data = data;
                             
                                var dsoptions = {
                                    schema: {
                                        model: {
                                            id: "Id",
                                            fields: { Id: { editable: false, nullable: true } }
                                        }
                                    },
                                    data: viewModel.m.Data,
                                };
                                var gridDataSource = new kendo.data.DataSource(dsoptions);
                                grid.setDataSource(gridDataSource);
                                gridDataSource.read();
                            }
                        }
                    }
                    catch (error) {
                        myAlert("格式错误：" + error)
                    }
                    kendo.ui.progress($("body"), false);
                };
                reader.readAsArrayBuffer(file);
                kendo.ui.progress($("body"), true);
            });

            function exportToExcelSJZD() {
                // 获取数据
                var data = viewModel.m.Data;

                // 创建工作表数据
                var wsData = [
                    ["名称", "低值", "高值", "报警状态"] // 表头
                ];

                // 填充数据
                data.forEach(function (item) {
                    wsData.push([
                        item.Name || "",
                        item.IMin || "",
                        item.IMax || "",
                        item.BJStatus || ""
                    ]);
                });

                // 创建工作表
                var ws = XLSX.utils.aoa_to_sheet(wsData);

                // 创建工作簿
                var wb = XLSX.utils.book_new();
                XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

                // 导出文件
                XLSX.writeFile(wb, "数据字典.xlsx");
            }
        </script>
        <script>
            var SizeInfo;
            $(function () {

                kendo.culture("zh-CN");


                if (window.Init) {
                    Init();
                }
            });
            function After50(d) {
                if (d) {
                    setTimeout(function () { d(); }, 50);
                }
            }
        </script>
</body>
</html>
